//
//  SearchViewController.m
//  逗她盒子
//
//  Created by Qiaofeng on 15/6/11.
//  Copyright (c) 2015年 Qiaofeng. All rights reserved.
//

#import "SearchViewController.h"
#import "AFNetworking.h"
#import "SearchModel.h"
#import "SearchTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "DataViewController.h"
#import "UserViewController.h"
@interface SearchViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)NSString *searchStr;
@property (nonatomic, strong)UITableView *table;
@end

@implementation SearchViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
         [self createSearchBar];
         [self createTable];
    }
    return self;
}
- (void)loadView
{
    [super loadView];
}

- (void)createSearchBar
{
    UISearchBar *searchBar = [[UISearchBar alloc]init];
    self.navigationItem.titleView = searchBar;
    searchBar.delegate = self;
    searchBar.placeholder = @"搜索昵称,ID,英雄,物品...";
    [searchBar becomeFirstResponder];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
   
    [searchBar resignFirstResponder];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    self.searchStr = searchText;
    [self json];
}




// --------------------- json 解析--------------------------------//
- (void)json
{
    self.playersModelArray = [[NSMutableArray alloc]init];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *url = [NSString stringWithFormat:@"http://api.maxjia.com/api/search/?"];
    [manager.requestSerializer setValue:@"http://api.maxjia.com/" forHTTPHeaderField:@"Referer"];
    NSDictionary *parameters = @{@"q":self.searchStr, @"phone_num":@"00000000000", @"pkey":@"randpkey", @"os_type":@"iOS",@"os_version":@"8.3",@"time":@"1433927795", @"version":@"1.0.0"};
    [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *BigDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *resultDic = [BigDic objectForKey:@"result"];
        NSArray *playersDic = [resultDic objectForKey:@"players"];
        for (NSDictionary *players in playersDic) {
            SearchModel *model = [[SearchModel alloc]init];
            [model setValuesForKeysWithDictionary:players];
            [self.playersModelArray addObject:model];
        }
        [self.table reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}


- (void)createTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height )];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    self.table.tableFooterView = [[UIView alloc]init];

}
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return 1;
//}
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//   NSString *a = @"玩家:100";
//    return a;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.playersModelArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellIdentfy = @"cell";
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentfy];
    if (!cell) {
        cell = [[SearchTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentfy];
    }
    SearchModel *model = [self.playersModelArray objectAtIndex:indexPath.row];
    NSURL *url = [NSURL URLWithString:model.avatar_url];
    [cell.avatar sd_setImageWithURL:url];
    cell.name.text = model.name;
    NSString *steam_id = [NSString stringWithFormat:@"ID: %@",model.steam_id];
    cell.steam_id.text = steam_id;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [self.navigationController setNavigationBarHidden:YES];
    SearchModel *model = [self.playersModelArray objectAtIndex:indexPath.row];
    UserViewController *user = [[UserViewController alloc]init];
    user.steam_id = model.steam_id;
    [self.navigationItem.titleView resignFirstResponder];
    [self.navigationController pushViewController:user animated:YES];

}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
     self.automaticallyAdjustsScrollViewInsets = NO;    // 关闭自适应 cell上面不会留白
    
   
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
